/** @file arena.c
    @brief Implementations of definitions in arena.h

    @author Juan Pablo Carbajal
    
    @date 21. July, 2008
    
*/

#include "arena.h"

void initArena(arena* arena)
{	arenaBorder* border = arena->getBorder();
	cpShape *aShape;
	string msg = "Build Arena with ";

	if (border->getUses().compare("none") != 0)
	{
	
// Border geometry
	msg += border->getUses();
	msg += " distributed ";
	msg += border->getDistribution();
	printf("%s.........",msg.c_str());
	if(border->getUses().compare("convpoly")==0)
	{
		perror("Arena convpoly: Not yet implemented");
		exit(1);
		/** @todo Implement convpoly Arena*/
		if(border->getDistribution().compare("circular")==0)
		{
			// Distributes the border shapes in a circunferece of radius param.radius
			//angle and direction of each shape					
			// get the offset for that angle
			// Rotate and scale each shape
		}
		else
		{
			perror("Arena custom distribution of convpoly: Not yet implemented");
			exit(1);
			/** @todo Implement custom distribution of shapes*/
		}
	}
	else if (border->getUses().compare("segment")==0)
	{
		if(border->getDistribution().compare("circular")==0)
		{
			// Distributes the border shapes in a circunferece of radius param.radius
			cpFloat angle, length;
			cpVect dir,offset;
		
			//amplitude and length of each shape
			angle=2.0*M_PI/border->getQuantity();

			//@TODO, andri: check if this is a shape or border property
			length=angle*border->getRadius()/cos(angle/2.0);
			/** @warning We are using paramArena.radius, 
			 it should be simplified.
			 */
			
			// Vertices of the master segment
			cpVect segm[2];
			segm[0]=cpv(0.0,0.5*length);
			segm[1]=cpv(0.0,-0.5*length);
			
			cpVect newshape[2];

			shape* shapes = border->getShapes()[1];

			for(int i=0; i<border->getQuantity();i++)
			{
				//direction of each segment			
				dir=cpvforangle(angle*i);
				// get the offset for that angle
				offset=cpvmult(dir,border->getRadius());
				// Rotate each segment
				newshape[0]=cpvadd(cpvrotate(segm[0],dir),offset);
				newshape[1]=cpvadd(cpvrotate(segm[1],dir),offset);
				aShape = cpSegmentShapeNew(staticBody,newshape[0], newshape[1],0.0);
				aShape->e = shapes->getElasticity();
				aShape->u = shapes->getFriction();
				cpSpaceAddStaticShape(space, aShape);
			}
		}
		else
		{
			perror("Arena custom distribution of segments: Not yet implemented");
			exit(1);
			/** @todo Implement custom distribution of segments*/
		}
	}
	else if (border->getUses().compare("circle")==0)
	{
		perror("Arena circle: Not yet implemented");
		exit(1);
		/** @todo Implement circle Arena*/
		
		if(border->getDistribution().compare("circular")!=0)
		{
			// Distributes the border shapes in a circunferece of radius param.radius
			//angle and direction of each shape					
			// get the offset for that angle
			// Rotate and scale each shape
		}
		else
		{
			perror("Arena custom distribution of circles: Not yet implemented");
			exit(1);
			/** @todo Implement custom distribution of circles*/
		}
	}
	else {
		perror("Arena shape type not recognized as valid shape type");
		exit(1);
	}
	}
	else
		printf("No arena loaded.........");
	printf("OK\n");
}
